﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using DataLib.Common;
using DataLib.Accounts.Bus;

namespace Ewts.BaseData
{
    public partial class CustomerAddressAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["cumid"] != null && Request["cumid"].Trim() != "")
            {
                this.hidcustomerid.Value = Request["cumid"];
                DataLib.Model.Cumtomer cmodel = new DataLib.Model.Cumtomer();
                cmodel = new DataLib.BLL.Cumtomer().GetModel(int.Parse(Request["cumid"].ToString().Trim()));
                txtcompanyname.Text = cmodel.companyname;
                txtCustomerCode.Text = cmodel.customercode;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (txtCustomerCode.Text.Trim().Length == 0)
            {
                strErr += "客户代码不能为空！\\n";
            }
            if (txtcompanyname.Text.Trim().Length == 0)
            {
                strErr += "客户名称不能为空！\\n";
            }
            if (txtCity.Text.Trim().Length == 0)
            {
                strErr += "客户城市不能为空！\\n";
            }
            if (txtaddress.Text.Trim().Length == 0)
            {
                strErr += "客户地址不能为空！\\n";
            }
            if (txtlinkman.Text.Trim().Length == 0)
            {
                strErr += "客户联系人不能为空！\\n";
            }
            int customerid = int.Parse(this.hidcustomerid.Value);
            DataLib.BLL.CumtomerAddress bllAdd = new DataLib.BLL.CumtomerAddress();
            DataTable dt = bllAdd.GetList(" customerid=" + customerid + " and address='" + txtaddress.Text + "'").Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                strErr += "地址不能重复\n";
            }
            //if (!PageValidate.IsNumber(txtphone.Text))  根据需求电话不需要验证  2013.04.17  梁玲
            //{
            //    strErr += "联系人电话不能为空且必须为数字！\\n";  
            //}

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }

            string companyname = this.txtcompanyname.Text;
            string city = this.txtCity.Text;
            string address = this.txtaddress.Text;
            string linkman = this.txtlinkman.Text;
            string phone = this.txtphone.Text;

            DataLib.Model.CumtomerAddress model = new DataLib.Model.CumtomerAddress();
            model.customerid = customerid;
            model.companyname = companyname;
            model.City = city;
            model.address = address;
            model.linkman = linkman;
            model.phone = phone;

            DataLib.BLL.CumtomerAddress bll = new DataLib.BLL.CumtomerAddress();
            bll.Add(model);
            if (Session["UserInfo"] != null)
            {
                User curUser = new DataLib.Accounts.Bus.User();
                curUser = (User)Session["UserInfo"];
                if (curUser.UserType == "AA")
                    DataLib.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "/Storage/MyCustomerAddList.aspx?cumid=" + customerid.ToString());
                else
                    DataLib.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "/Storage/MyCustomerAddList.aspx?cumid=" + customerid.ToString());
            }

           

        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            int customerid = int.Parse(this.hidcustomerid.Value);
            Response.Redirect("/Storage/MyCustomerList.aspx?cumid=" + customerid.ToString());
        }

    }
}
